package com.mk.controller.admin;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.mk.constant.JwtClaimsConstant;
import com.mk.dto.OfficerLoginDTO;
import com.mk.entity.Officer;
import com.mk.properties.JwtProperties;
import com.mk.result.Result;
import com.mk.service.IOfficerService;
import com.mk.utils.JwtUtil;
import com.mk.vo.OfficerLoginVO;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 客户端执法人员登录
 */
@RestController
@RequestMapping("/admin/officer")
@RequiredArgsConstructor
public class OfficerController {

    private final IOfficerService officerService;

    @Resource
    private JwtProperties jwtProperties;

    private final JwtUtil jwtUtil;

//    //延迟初始化JwtUtil
//    @PostConstruct
//    public void init() {
//        this.jwtUtil = new JwtUtil(jwtProperties.getUserSecretKey());
//    }
    /**
     * 登录
     * @param officerLoginDTO
     * @return
     */
    @CrossOrigin
    @PostMapping("/login")
    public Result<OfficerLoginVO> login(@RequestBody OfficerLoginDTO officerLoginDTO) {
        Officer officer = officerService.login(officerLoginDTO);

        //登录成功后，生成jwt令牌
//        Map<String, Object> claims = new HashMap<>();
//        claims.put(JwtClaimsConstant.OFFICER_ID, officer.getId());
        String token = jwtUtil.generateToken(officer.getUsername(), jwtProperties.getTtl());

        if (officer.getCount() == 0) {
            officerService.update(new LambdaUpdateWrapper<Officer>()
                    .set(Officer::getCount, officer.getCount() + 1)
                    .eq(Officer::getId, officer.getId()));
            return Result.error("用户为第一次登录，请修改密码, token为: " + token);
        } else {
            officerService.update(new LambdaUpdateWrapper<Officer>()
                    .set(Officer::getCount, officer.getCount() + 1)
                    .eq(Officer::getId, officer.getId()));

            OfficerLoginVO officerLoginVO = OfficerLoginVO.builder()
                    .id(officer.getId())
                    .username(officer.getUsername())
                    .name(officer.getName())
                    .token(token)
                    .build();

            return Result.success(officerLoginVO);
        }
    }

    /**
     * 退出
     *
     * @return
     */
    @CrossOrigin
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }


    /**
     * 根据id查询执法人员信息
     * @param id
     * @return
     */
    @CrossOrigin
    @GetMapping("/{id}")
    public Result<Officer> getById(@PathVariable Long id) {
        Officer officer = officerService.getById(id);
        return Result.success(officer);
    }

    /**
     * 编辑信息
     * @param officer
     * @return
     */
    @CrossOrigin
    @PutMapping("/update")
    public Result update(@RequestBody Officer officer) {
        officer.setUpdateTime(LocalDateTime.now());
        officerService.updateById(officer);
        return Result.success();
    }
}
